//
//  YWMorePeopleViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWMorePeopleViewController.h"
#import "YWFansModel.h"
#import "YWFansTableViewCell.h"
#import "YWAnchorIntroduceViewController.h"

@interface YWMorePeopleViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *morePeopleTableView;

@property (nonatomic, retain) NSMutableArray *morePeopleArray;

@property (nonatomic, assign) NSInteger maxPageId;

@property (nonatomic, assign) NSInteger pageId;

@end

@implementation YWMorePeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getTitle];
    [self getView];
    self.morePeopleArray = [NSMutableArray array];
    _pageId = 1;
    [self getDataByPageId:_pageId];
}

- (void)getTitle
{
    if ([_typeString isEqualToString:@"following"]) {
        self.navigationItem.title = @"关注的人";
    }
    else
    {
        self.navigationItem.title = @"粉丝";
    }
}

- (void)getView
{
    self.morePeopleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    _morePeopleTableView.delegate = self;
    _morePeopleTableView.dataSource = self;
    _morePeopleTableView.separatorStyle = NO;
    [self.view addSubview:_morePeopleTableView];
    
    [_morePeopleTableView registerClass:[YWFansTableViewCell class] forCellReuseIdentifier:@"fansCell"];
    [_morePeopleTableView registerClass:[YWFansTableViewCell class] forCellReuseIdentifier:@"faultfansCell"];
    
    _morePeopleTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_morePeopleArray removeAllObjects];
        [_morePeopleTableView reloadData];
        _pageId = 1;
        [self getDataByPageId:_pageId];
    }];
    _morePeopleTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageId++;
        if (_pageId > _maxPageId) {
            [_morePeopleTableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [self getDataByPageId:_pageId];
        }
    }];
    //[_morePeopleTableView.mj_footer beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _morePeopleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWFansModel *fansModel = [_morePeopleArray objectAtIndex:indexPath.row];
    if (fansModel.isVerified == NO) {
        return 60;
    }
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWFansModel *fansModel = [_morePeopleArray objectAtIndex:indexPath.row];
    if (fansModel.isVerified == YES) {
        YWFansTableViewCell *fansCell = [tableView dequeueReusableCellWithIdentifier:@"fansCell"];
        fansCell.fansModel = fansModel;
        return fansCell;
    }
    else
    {
        YWFansTableViewCell *fansCell = [tableView dequeueReusableCellWithIdentifier:@"faultfansCell"];
        fansCell.fansModel = fansModel;
        return fansCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWFansModel *fansModel = [_morePeopleArray objectAtIndex:indexPath.row];
    YWAnchorIntroduceViewController *ivc = [[YWAnchorIntroduceViewController alloc] init];
    ivc.uid = fansModel.uid;
    [self.navigationController pushViewController:ivc animated:YES];
}

- (void)getDataByPageId:(NSInteger)pageId
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/%@?device=iPhone&pageId=%ld&toUid=%ld", _typeString, pageId, _uid];
    NSLog(@"%@", urlString);
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        _maxPageId = [[result objectForKey:@"maxPageId"] integerValue];
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWFansModel *fansModel = [[YWFansModel alloc] initWithDic:dic];
            [_morePeopleArray addObject:fansModel];
        }
        [_morePeopleTableView reloadData];
        [_morePeopleTableView.mj_header endRefreshing];
        [_morePeopleTableView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
